package com.zeta.base;



import java.util.Arrays;
import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestParam;

import com.baomidou.mybatisplus.extension.service.IService;
import com.zeta.commonutils.R;

public abstract class BaseController<T> {

	public abstract IService<T> getBaseService();
	
	 @PostMapping("")
	    public R add(@RequestBody T object){

	       boolean result = getBaseService().save(object);
	   
	       if(result) {
	        	 return R.ok();
	        }
	        return R.error();
	    }
	 
	 
	    @DeleteMapping("/{id}")
	    public R delete(@PathVariable String id){
	    
	        boolean result = getBaseService().removeById(id);
	        if(result) {
	        	 return R.ok();
	        }
	        return R.error();
	    }
	 
	    /**
	     * 通过主键id批量删除 参数必须为数组，名为ids
	     * @param ids
	     * @return
	     */
	    @DeleteMapping("/deletes")
	    public Object batchDelete(@RequestParam("ids[]") Integer ids[]){
	    
	        boolean result = getBaseService().removeByIds(Arrays.asList(ids));
	        if(result) {
	        	 return R.ok();
	        }
	        return R.error();
	    }
	 
	 
	 
	    @PutMapping("")
	    public Object update(@RequestBody T object){
	      boolean result = getBaseService().update(object, null);
	      
	      if(result) {
	        	 return R.ok();
	        }
	        return R.error();
	 
	    }
	    @GetMapping("{id}")
	    public Object get(@PathVariable String id){
	         T result = getBaseService().getById(id);
	      
	        if(result!=null) {
	        	 return R.ok().data("object",result);
	        }
	        return R.error();
	    }
	    @GetMapping("/all")
	    public Object getAll(){
	         List<T> result = getBaseService().list(null);
	      
	        if(result!=null) {
	        	 return R.ok().data("list",result);
	        }
	        return R.error();
	    }
	 
	 
	 
	    /**
	     * 附带检索条件的分页查询
	     * @param map
	     * @param pageNo
	     * @param pageSize
	     * @return
	     */
//	    @RequestMapping("getPageQuery")
//	    public Object getListByPage(@RequestBody Map map, @RequestParam("pageNo") int pageNo, @RequestParam("pageSize") int pageSize){
//	        T t = getBaseService().newinstance(map);
//	        PageInfo<T> pageInfo =getBaseService().queryListByPage(t,pageNo,pageSize);
//	        return success(pageInfo,"");
//	    }
//	 
//	    /**
//	     * 无检索条件的分页查询
//	     * @param pageNo
//	     * @param pageSize
//	     * @return
//	     */
//	    @RequestMapping("getPage")
//	    public Object getListByPage( @RequestParam("pageNo") int pageNo, @RequestParam("pageSize") int pageSize){
//	        //T t = getBaseService().newinstance(null);
//	        T t=null;
//	        PageInfo<T> pageInfo =getBaseService().queryListByPage(t,pageNo,pageSize);
//	        return success(pageInfo,"");
//	    }


}
